package DoublePointer;

import org.junit.Test;

import java.util.Arrays;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: DoublePointer
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-03  17:51
 * @Description: TODO
 * @Version: 1.0
 */
public class 移动零283 {

    public void moveZeroes(int[] nums) {
        if (nums.length == 1) {
            return;
        }
        for (int i = 0; i < nums.length; i++) {
           for (int j=i+1;j<nums.length;j++){
               if (nums[i]==0&&nums[j]!=0){
                   int tmp=nums[i];
                   nums[i]=nums[j];
                   nums[j]=tmp;
               }
           }
        }
    }

    public void moveZeroes2(int[] nums) {
        if (nums.length == 1) {
            return;
        }
        int fast=1;
        int slow=0;
        while (fast<nums.length){
            if (nums[slow]==0&&nums[fast]!=0){
                int tmp=nums[slow];
                nums[slow]=nums[fast];
                nums[fast]=tmp;
                slow++;
            }else if (nums[slow]==0&&nums[fast]==0){
                fast++;
            }else {
                slow++;
                fast++;
            }
        }
        System.out.println(Arrays.toString(nums));
    }

    public void moveZeroes3(int[] nums) {
        int n = nums.length;
        int slow = 0, fast = 0;
        while (fast < n) {
            if (nums[fast] != 0) {
                int temp = nums[slow];
                nums[slow] = nums[fast];
                nums[fast] = temp;
                slow++;
            }
            fast++;
        }
        System.out.println(Arrays.toString(nums));
    }

    @Test
    public void test() {
        moveZeroes2(new int[]{2,1});
    }
}
